﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TotalSystems.Web.Services.Store;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using TotalSystems.DataAccess;
using TotalSystems.Store.Views.BaseInfo;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using TotalSystems.Core.Infrastructure;
using TotalSystems.Business.Entitys.Store;

namespace TotalSystems.Store.ViewModels.BaseInfo
{
    public class DegreeImportantStuff_VM : ViewModelBase
    {
        DegreeImportantStuffContext _Context = new DegreeImportantStuffContext();
        #region Constractor

        public DegreeImportantStuff_VM()
        {
            _Context.Load(_Context.GetDegreeImportantStuffQuery(), OnGetDegreeImportantStuffQueryCompleted, null);
            //DegreeImportantStuffEntity = new STR_DegreeImportantStuff {STR_Dis_Name="no name" };
            UserAddedNodes = new ObservableCollection<STR_DegreeImportantStuff>();
            UserEditedNodes = new ObservableCollection<STR_DegreeImportantStuff>();
            UserRemovedNodes = new ObservableCollection<STR_DegreeImportantStuff>();

            UpdateCommand = new RelayCommand(Update_Command);
            InsertCommand = new RelayCommand(Insert_Command);
            SearchCommand = new RelayCommand(Search_Command);
        }

        #endregion Defenition

        //BaseDefenitionTreeViewBuilder<STR_DegreeImportantStuff> TreeViewBuilder = new BaseDefenitionTreeViewBuilder<STR_DegreeImportantStuff>();
      
        #region Command

        public ICommand UpdateCommand { get; set; }
        public ICommand InsertCommand { get; set; }
        public ICommand SearchCommand { get; set; }

        #endregion

        #region Method Command

        private void Update_Command()
        {
            //Global.CopyTo(_testentity, DegreeImportantStuffEntity);
            _testentity.Copy(DegreeImportantStuffEntity);
            _Context.UpdateDegreeImportantStuff(DegreeImportantStuffEntity, OnUpdateDegreeImportantStuffCompleted, null);
        }

        private void Insert_Command()
        {
            
        }

        private void Search_Command()
        {
            _testentity = new DegreeImportantStuffWrapper();
            _testentity.Copy(DegreeImportantStuffEntity);

            var tt = new STR_DegreeImportantStuff();
            tt.Copy(DegreeImportantStuffEntity);

        }


        #endregion


        #region Method

        private void OnUpdateDegreeImportantStuffCompleted(InvokeOperation op)
        {

            if (op.Value != null)
                return;
        }

        #endregion

        #region Propertis

        public ObservableCollection<STR_DegreeImportantStuff> UserAddedNodes { get; set; }

        public ObservableCollection<STR_DegreeImportantStuff> UserEditedNodes { get; set; }

        public ObservableCollection<STR_DegreeImportantStuff> UserRemovedNodes { get; set; }

        private ObservableCollection<STR_DegreeImportantStuff> _degreeImportantStuffEntitys;
        public ObservableCollection<STR_DegreeImportantStuff> DegreeImportantStuffEntitys
        {
            get { return _degreeImportantStuffEntitys; }
            set
            {
                _degreeImportantStuffEntitys = value;
                RaisePropertyChanged("DegreeImportantStuffEntitys");
            }
        }

        private STR_DegreeImportantStuff _selectedDegreeImportantStuffEntitys;
        public STR_DegreeImportantStuff SelectedDegreeImportantStuffEntitys
        {
            get { return _selectedDegreeImportantStuffEntitys; }
            set
            {
                _selectedDegreeImportantStuffEntitys = value;
                RaisePropertyChanged("SelectedDegreeImportantStuffEntitys");
                DegreeImportantStuffEntity = _selectedDegreeImportantStuffEntitys;
                UpdateTVCanExecuteCommands();
            }
        }

        private STR_DegreeImportantStuff _degreeImportantStuffEntity;
        public STR_DegreeImportantStuff DegreeImportantStuffEntity
        {
            get { return _degreeImportantStuffEntity; }
            set
            {
                _degreeImportantStuffEntity = value;
                RaisePropertyChanged("DegreeImportantStuffEntity");
            }
        }

        private bool _canUserRemoveNode;
        public bool CanUserRemoveNode
        {
            get { return _canUserRemoveNode; }
            set { if (_canUserRemoveNode != value) { _canUserRemoveNode = value; RaisePropertyChanged("CanUserRemoveNode"); } }
        }

        private bool _canUserEditNode;
        public bool CanUserEditNode
        {
            get { return _canUserEditNode; }
            set { if (_canUserEditNode != value) { _canUserEditNode = value; RaisePropertyChanged("CanUserEditNode"); } }
        }

        private DegreeImportantStuffWrapper _testentity;
        #endregion

        #region 

        private void  OnGetDegreeImportantStuffQueryCompleted(LoadOperation<STR_DegreeImportantStuff> op)
        {
            DegreeImportantStuffEntitys = new ObservableCollection<STR_DegreeImportantStuff>(_Context.STR_DegreeImportantStuffs);



        }

        private void UpdateTVCanExecuteCommands()
        {
            if (SelectedDegreeImportantStuffEntitys != null)
            {
                if (Math.Abs(SelectedDegreeImportantStuffEntitys.STR_Dis_ID) == 1)
                {
                    CanUserEditNode = CanUserRemoveNode = false;
                }
                else
                    CanUserEditNode = CanUserRemoveNode = true;
            }
        }

        #endregion
    }
}
